package com.lagou.homework1_4;

import java.util.concurrent.locks.ReentrantLock;

public class ThreadTicket extends Thread {
    static int tickets = 0;

    @Override
    public void run() {
        while (true) {
            synchronized(RunnableTicket.class) { //同一个锁引用
                if (tickets >= 100) {
                    break;
                }

                System.out.println("子线程" + Thread.currentThread().getId() + ":" + Thread.currentThread().getName() + " 卖出第" + (tickets + 1) + "张票");
                tickets++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        ThreadTicket tt = new ThreadTicket();
        Thread t1 = new Thread(tt,"窗口一");
        Thread t2 = new Thread(tt,"窗口二");
        Thread t3 = new Thread(tt,"窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}
